/**
 * 
 */
package org.codesmell.util.rendermap.js;

import java.util.LinkedHashMap;

import org.codesmell.util.rendermap.MapRenderer;


/**
 * @author Uwe Schäfer, (schaefer@thomas-daily.de)
 */
class JSOptionMapRenderer implements MapRenderer
{

    @Override
    public String render(final LinkedHashMap<String, String> map)
    {
        StringBuffer sb = new StringBuffer(128);

        sb.append("{");
        for (String key : map.keySet())
        {
            String value = map.get(key);
            if (value != null)
            {
                if (sb.length() > 1)
                {
                    sb.append(", ");
                }
                sb.append(escaped(key) + " : " + value);
            }
        }
        sb.append("}");
        return sb.toString();
    }

    private static String escaped(final String value)
    {
        return value.replaceAll("'", "\\'");
    }

}
